﻿// TcpipDlg.cpp: 实现文件
//

#include "pch.h"
#include "TcpipDlg.h"
#include "afxdialogex.h"



// CTcpipDlg 对话框

IMPLEMENT_DYNAMIC(CTcpipDlg, CDialogEx)

CTcpipDlg::CTcpipDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_TCPIP, pParent)
	, m_nPort(0)
	, m_logBuffer(500)
{

}

CTcpipDlg::~CTcpipDlg()
{
}

void CTcpipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS, m_ipTcpipAddress);
	DDX_Control(pDX, IDC_LIST_RECIVE_INF, m_listTcpReciveInf);
	DDX_Control(pDX, IDC_EDIT_SEND_INF, m_editSendInf);
	DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
	DDX_Control(pDX, IDC_COMBO_MODE, m_comboMode);
}


BEGIN_MESSAGE_MAP(CTcpipDlg, CDialogEx)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_RECIVE_INF, &CTcpipDlg::OnNMCustomdrawListLog)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_RECIVE_INF, &CTcpipDlg::OnLvnItemchangedListLog)
	ON_MESSAGE(WM_SEND_LOG_MSG_TO_TCPIP_UI, &CTcpipDlg::OnReceiveAndDisplaySystemLog)
	ON_BN_CLICKED(IDC_BUTTON_SEND, &CTcpipDlg::OnBnClickedButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CTcpipDlg::OnBnClickedButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CTcpipDlg::OnBnClickedButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CTcpipDlg::OnBnClickedButtonSave)
END_MESSAGE_MAP()


// CTcpipDlg 消息处理程序


BOOL CTcpipDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	int nStyle;
	// TODO: 在此添加额外的初始化代码
	m_listTcpReciveInf.SetBkColor(RGB(200, 220, 250));
	//m_listTcpReciveInf.SetBkColor(TRANSPARENT);
	//m_listTcpReciveInf.SetTextColor(RGB(250, 250, 250));
	nStyle = GetWindowLong(m_listTcpReciveInf.m_hWnd, GWL_STYLE);
	nStyle &= ~LVS_TYPEMASK;
	nStyle |= LVS_REPORT;
	SetWindowLong(m_listTcpReciveInf.m_hWnd, GWL_STYLE, nStyle);
	m_listTcpReciveInf.ShowWindow(SW_SHOW);
	DWORD dwStyle = m_listTcpReciveInf.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_INFOTIP;

	m_listTcpReciveInf.SetExtendedStyle(dwStyle);
	m_listTcpReciveInf.SetFont(&gSystemMgr.m_logFont);
	m_listTcpReciveInf.EnableWindow(TRUE);
	m_listTcpReciveInf.InsertColumn(0, L"当前时刻", LVCFMT_LEFT, 300 / 10 * 3);
	m_listTcpReciveInf.InsertColumn(1, L"接收信息", LVCFMT_LEFT, 300 / 10 * 20);
	m_listTcpReciveInf.ShowWindow(SW_HIDE);
	m_listTcpReciveInf.EnableWindow(TRUE);
	m_listTcpReciveInf.Invalidate();
	m_listTcpReciveInf.ShowWindow(SW_SHOW);
	m_listTcpReciveInf.BringWindowToTop();

	m_comboMode.InsertString(0, L"Server");
	m_comboMode.InsertString(1, L"Client");
	m_comboMode.SetCurSel(0);

	SetUiHandle();
	ReadConfigToEditValue();
	//GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CTcpipDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}

void CTcpipDlg::ReadConfigToEditValue()
{
	m_ipTcpipAddress.SetWindowTextW(gSystemMgr.m_sTcpIpInf.m_cstrIpAddress);
	m_nPort = gSystemMgr.m_sTcpIpInf.m_nPort;
	m_comboMode.SetCurSel(gSystemMgr.m_sTcpIpInf.m_nComboMode);

	UpdateData(false);
}
void CTcpipDlg::ReadEditValueToConfig()
{
	CString cstr;
	UpdateData(TRUE);

	m_ipTcpipAddress.GetWindowTextW(cstr);
	gSystemMgr.m_sTcpIpInf.m_cstrIpAddress = cstr;
	gSystemMgr.m_cTcpConfigIni.SaveConfigIni(L"TcpIpConfig", L"m_cstrIpAddress", cstr);

	gSystemMgr.m_sTcpIpInf.m_nComboMode = m_comboMode.GetCurSel();
	gSystemMgr.m_sTcpIpInf.m_nPort = m_nPort;

	cstr.Format(L"%d", gSystemMgr.m_sTcpIpInf.m_nComboMode);
	gSystemMgr.m_cTcpConfigIni.SaveConfigIni(L"TcpIpConfig", L"m_nComboMode", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sTcpIpInf.m_nPort);
	gSystemMgr.m_cTcpConfigIni.SaveConfigIni(L"TcpIpConfig", L"m_nPort", cstr);

}
void CTcpipDlg::OnBnClickedButtonSend()
{
	CString cstrLog;
	UpdateData(TRUE);
	m_editSendInf.GetWindowTextW(gSystemMgr.m_sTcpIpInf.m_cstrSendInf);
	gSystemMgr.TcpIpSendMessage();

	if (ERROR_SEND_PACK_FAIL == gSystemMgr.m_sTcpIpInf.m_nDataSize)
	{
		gSystemMgr.m_cTcpIp.DisplayLogAndSave(L"数据发送失败！", LV_ERROR);
	}
	else
	{
		cstrLog.Format(L"S[%2d]:%s", gSystemMgr.m_sTcpIpInf.m_cstrSendInf.GetLength(), gSystemMgr.m_sTcpIpInf.m_cstrSendInf);
		gSystemMgr.m_cTcpIp.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	}

}

void CTcpipDlg::SetUiHandle()
{
	gSystemMgr.m_ptrTcpIpUI = this;
	gSystemMgr.m_cTcpIp.m_hUiHandle = this->GetSafeHwnd();
}

void CTcpipDlg::OnBnClickedButtonConnect()
{
	CString strIp;
	UpdateData(TRUE);

	gSystemMgr.m_sTcpIpInf.m_nComboMode = m_comboMode.GetCurSel();
	m_ipTcpipAddress.GetWindowTextW(gSystemMgr.m_sTcpIpInf.m_cstrIpAddress);
	gSystemMgr.m_sTcpIpInf.m_nPort = m_nPort;
	gSystemMgr.TcpIpConnect();
}

LRESULT CTcpipDlg::OnReceiveAndDisplayTCPInf(WPARAM wParam, LPARAM lParam)
{
	eEVENT_LEVEL   RecLevel = LV_EVENT;
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);
	switch (lParam)
	{
	case(LV_EVENT):
		RecLevel = LV_EVENT;
		break;
	case(LV_WARNING):
		RecLevel = LV_WARNING;
		break;
	case(LV_ERROR):
		RecLevel = LV_ERROR;
		break;
	default:
		break;
	}
	wstring wstrLogMsg = cstrp.GetString();
	std::vector<wstring> vLogItem = _utils::SplitString(wstrLogMsg, L"#");
	if (vLogItem.size() != 2)
	{
		return 0;
	}
	wstring wstrTime = vLogItem[0];
	wstring wstrMsg = vLogItem[1];
	int nIndexItem = m_listTcpReciveInf.GetItemCount();
	m_listTcpReciveInf.InsertItem(0, L"");
	m_listTcpReciveInf.SetItemText(0, 0, wstrTime.c_str());
	m_listTcpReciveInf.SetItemText(0, 1, wstrMsg.c_str());
	LOG_PACKAGE RecPackage;
	RecPackage.m_level = RecLevel;
	RecPackage.m_wstrTime = wstrTime;
	RecPackage.m_wstrMsg = wstrMsg;
	m_TcpRecBuffer.push_back(RecPackage);
	m_listTcpReciveInf.Invalidate();
	if (nIndexItem >= 500)
	{
		m_listTcpReciveInf.DeleteItem(500);
	}
	return 0;
}

void CTcpipDlg::OnBnClickedButtonClose()
{
	switch (m_comboMode.GetCurSel())
	{
	case 0:
		gSystemMgr.m_cTcpIp.CloseSocketServer();

		break;
	case 1:
		gSystemMgr.m_cTcpIp.CloseClient();
		break;
	default:
		break;
	}
}

LRESULT CTcpipDlg::OnReceiveAndDisplaySystemLog(WPARAM wParam, LPARAM lParam)
{
	eEVENT_LEVEL   logLevel = LV_EVENT;;
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);
	switch (lParam)
	{
	case(LV_EVENT):
		logLevel = LV_EVENT;
		break;
	case(LV_WARNING):
		logLevel = LV_WARNING;
		break;
	case(LV_ERROR):
		logLevel = LV_ERROR;
		break;
	default:
		break;
	}
	wstring wstrLogMsg = cstrp.GetString();
	std::vector<wstring> vLogItem = _utils::SplitString(wstrLogMsg, L"#");
	if (vLogItem.size() != 2)
	{
		return 0;
	}
	wstring wstrTime = vLogItem[0];
	wstring wstrMsg = vLogItem[1];
	int nIndexItem = m_listTcpReciveInf.GetItemCount();
	m_listTcpReciveInf.InsertItem(0, L"");
	m_listTcpReciveInf.SetItemText(0, 0, wstrTime.c_str());
	m_listTcpReciveInf.SetItemText(0, 1, wstrMsg.c_str());
	LOG_PACKAGE logPackage;
	logPackage.m_level = logLevel;
	logPackage.m_wstrTime = wstrTime;
	logPackage.m_wstrMsg = wstrMsg;
	m_logBuffer.push_back(logPackage);
	m_listTcpReciveInf.Invalidate();
	if (nIndexItem >= 500)
	{
		m_listTcpReciveInf.DeleteItem(500);
	}
	return 0;
}

void CTcpipDlg::OnLvnItemchangedListLog(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}

void CTcpipDlg::OnNMCustomdrawListLog(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* lplvcd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	int nIRow = int(lplvcd->nmcd.dwItemSpec);
	if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
		return;
	}
	if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
	{
		lplvcd->clrTextBk = RGB(200, 220, 250);
		//lplvcd->clrTextBk =TRANSPARENT;
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		return;
	}
	else if (lplvcd->nmcd.dwDrawStage == (CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEMPREPAINT))
	{
		COLORREF crText = RGB(10, 10, 10);
		if (lplvcd->iSubItem == 1)
		{
			LOG_PACKAGE logPackage;
			logPackage = m_logBuffer.at(m_listTcpReciveInf.GetItemCount() - nIRow - 1);
			switch (logPackage.m_level)
			{
			case(LV_ERROR):
				crText = RGB(250, 0, 0);
				break;
			case(LV_WARNING):
				crText = RGB(150, 150, 0);
				break;
			case(LV_EVENT):
				crText = RGB(50, 50, 50);
				break;
			default:
				break;
			}
			lplvcd->clrText = crText;
			*pResult = CDRF_DODEFAULT;
			return;
		}
	}
	*pResult = 0;
}

void CTcpipDlg::OnBnClickedButtonSave()
{
	ReadEditValueToConfig();
}

